package vm.baseNativeUseFunction;

import java.io.*;

public class FileToObjectConverter implements Serializable{

    public static Object fileAsObject(String path) throws IOException, ClassNotFoundException {
        // 创建一个文件输入流
        FileInputStream fileInputStream = new FileInputStream(path);
        // 创建一个对象输入流
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        try {
            // 从对象输入流中读取对象
            return objectInputStream.readObject();
        } finally {
            // 关闭流
            objectInputStream.close();
            fileInputStream.close();
        }
    }
    public static void objectToFile(String path, Object object) throws IOException {
        // 创建一个文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream(path);
        // 创建一个对象输出流
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        try {
            // 将对象写入到对象输出流中
            objectOutputStream.writeObject(object);
        } finally {
            // 关闭流
            objectOutputStream.close();
            fileOutputStream.close();
        }
    }
}
